﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Testing.Models
{
    public class DatePickerModel
    {
        private readonly DateTimeFormatInfo DateTimeFormat = CultureInfo.CurrentUICulture.DateTimeFormat;
        public List<PickerData> Years { get; private set; }
        public List<PickerData> Months { get; private set; }



        public DatePickerModel()
            : base()
        {
            CreateMonths();
            CreateYears();
        }


        private void CreateYears()
        {
            List<PickerData> list = new List<PickerData>();
            for (int y = 1900; y < 2100; y++)
            {
                PickerData data = new PickerData("", y.ToString(), y);
                list.Add(data);
            }
            Years = list;
        }

        private void CreateMonths()
        {
            string[] monthNames = DateTimeFormat.MonthNames;
            List<PickerData> list = new List<PickerData>();
            for (int m = 1; m <= 12; m++)
            {
                PickerData data = new PickerData(monthNames[m - 1], m.ToString(), m);
                list.Add(data);
            }
            Months = list;
        }

        public List<PickerData> GetDays(int month, int year)
        {
            string[] dayNames = DateTimeFormat.DayNames;
            int days = DateTime.DaysInMonth(year, month);
            List<PickerData> list = new List<PickerData>();
            DateTime date = new DateTime(year, month, 1);
            for (int d = 1; d <= days; d++)
            {
                PickerData data = new PickerData(dayNames[(int)date.DayOfWeek], d.ToString(), d);
                date = date.AddDays(1);
                list.Add(data);
            }
            return list;
        }

        /// <summary>
        /// Gets the year from the given index.
        /// </summary>
        public int GetYearFromIndex(int index)
        {
            return index + 1900;
        }

        public int GetIndexFromYear(int year)
        {
            return year - 1900;
        }

        /// <summary>
        /// Gets the month from 1..12 from the given index.
        /// </summary>
        public int GetMonthFromIndex(int index)
        {
            return index + 1;
        }

        public int GetIndexFromMonth(int month)
        {
            return month - 1;
        }

        public int GetIndexFromDay(int day)
        {
            return day - 1;
        }

        public int GetDayFromIndex(int index)
        {
            return index + 1;
        }

        public int GetDayIndex()
        {
            string pattern = DateTimeFormat.ShortDatePattern;
            int m = pattern.IndexOf("m", StringComparison.InvariantCultureIgnoreCase);
            int y = pattern.IndexOf("y", StringComparison.InvariantCultureIgnoreCase);
            int d = pattern.IndexOf("d", StringComparison.InvariantCultureIgnoreCase);
            if (d < y && d < m) return 0;
            return d < y ? 1 : 2;
        }

        public int GetYearIndex()
        {
            string pattern = DateTimeFormat.ShortDatePattern;
            int m = pattern.IndexOf("m", StringComparison.InvariantCultureIgnoreCase);
            int y = pattern.IndexOf("y", StringComparison.InvariantCultureIgnoreCase);
            int d = pattern.IndexOf("d", StringComparison.InvariantCultureIgnoreCase);
            if (y < m && y < d) return 0;
            return y < m ? 1 : 2;
        }

        public int GetMonthIndex()
        {
            string pattern = DateTimeFormat.ShortDatePattern;
            int m = pattern.IndexOf("m", StringComparison.InvariantCultureIgnoreCase);
            int y = pattern.IndexOf("y", StringComparison.InvariantCultureIgnoreCase);
            int d = pattern.IndexOf("d", StringComparison.InvariantCultureIgnoreCase);
            if (m < y && m < d) return 0;
            return m < y ? 1 : 2;
        }

    }
}
